import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    name: '', // 用户名称
    userId: '',
    avatar: '',
    roles: [],
    permissions: [],
    level:'',
    templateId:'',
    id:'',
    parentId: '',
    nickName:'',
    deptId:'',
    deptName: '', // 部门名称
    tenantName: '', //租户名称
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_USERID: (state, userId) => {
      state.userId = userId
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_LEVEL: (state, level) => {
      state.level = level
    },
    SET_TEMPLATE: (state, templateId) => {
      state.templateId = templateId
    },
    SET_TENANT: (state,id) => {
      state.id = id
    },
    SET_NICKNAME: (state,nickName) => {
      state.nickName = nickName
    },
    SET_DEPTID: (state,deptId) => {
      state.deptId = deptId
    },
    SET_DEPTNAME: (state, deptName) => {
      state.deptName = deptName;
    },
    SET_TENANTNAME: (state, tenantName) => {
      state.tenantName = tenantName;
    },
    SET_PARENTID: (state, parentId) => {
      state.parentId = parentId;
    },
    SET_TYPE: (state, type) => {
      state.type = type;
    }
    // parentId
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      // const code = userInfo.code
      const uuid = userInfo.uuid
      return new Promise((resolve, reject) => {
        login(username, password, uuid).then(res => {
          console.log('loginres=', res);
          setToken(res.data)
          commit('SET_TOKEN', res.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          console.log('userinfo=', res);
          const user = res.data.user
          // console.log('user=', user);
          const tenant = res.data.tenant
          const avatar = (user.avatar == "" || user.avatar == null) ? '1' : user.avatar;
          if (res.data.roles && res.data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', res.data.roles)
            // commit('SET_PERMISSIONS', res.data.permissions)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          // console.log('res.data.permissions=', res.data.permissions);
          commit('SET_PERMISSIONS', res.data.permissions)
          commit('SET_NAME', user.userName)
          commit('SET_USERID', user.userId)
          commit('SET_NICKNAME', user.nickName)
          commit('SET_DEPTID', user.deptId)
          if (user && user.dept) {
            commit('SET_DEPTNAME', user.dept.deptName);
          }
          commit('SET_AVATAR', avatar)
          // commit('SET_LEVEL', tenant.level)
          // commit('SET_TEMPLATE', tenant.templateId)
          // commit('SET_TENANT', tenant.id)
          // commit('SET_TENANTNAME', tenant.name)
          // commit('SET_PARENTID', tenant.parentId)
          // commit('SET_TYPE', tenant.type)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
